﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace WindowsFormsApplication3
{
    public partial class QQ : Form
    {
        //数据库连接
      private  SqlConnection conn = null;
        public QQ()
        {
            InitializeComponent();
        }
        //连接数据库
        private void button1_Click(object sender, EventArgs e)
        {
            //数据连接信息
            String connStr = "Data source = .;Initial Catalog = learn;User ID = sa;Password = 123456";
            //创建连接
           
            try
            {
                conn = new SqlConnection(connStr);
                //打开数据库
                conn.Open();
                MessageBox.Show("数据库连接成功");
            }
            catch (Exception ex)
            {
                MessageBox.Show("数据库链接失败" + ex.Message);
              
            }
           
        }
        //注册账号
        private void button3_Click(object sender, EventArgs e)
        {
            String sql = "insert into QQ(name,pwd) values('{0}','{1}')";
            sql = String.Format(sql, textBox4.Text, textBox3.Text);
            SqlCommand cmd = new SqlCommand(sql, conn);

            try
            {
                int i = cmd.ExecuteNonQuery();
                if (i != -1)
                {
                    MessageBox.Show("注册成功");
                    label5.Text = "账号:" + textBox4.Text + "密码:" + textBox3.Text + " " + "请妥善保管";
                }
            }
            catch(Exception ex)
            {
                MessageBox.Show("注册失败", ex.Message);
            }
           
            
        }
        //关闭数据库
        private void button4_Click(object sender, EventArgs e)
        {
            if(conn != null)
            {
                conn.Close();
                MessageBox.Show("关闭成功");
            }
        }
        //登录
        private void button2_Click(object sender, EventArgs e)
        {
            String sql = "select count(*) from QQ where name = '{0}' and pwd = '{1}'";
            sql = String.Format(sql, textBox1.Text, textBox2.Text);
            SqlCommand cmd = new SqlCommand(sql, conn);
            try
            {
                int i = (int)cmd.ExecuteScalar();
                if(i != 0)
                {
                    MessageBox.Show("登录成功");
                }else
                {
                    MessageBox.Show("账号或密码错误");
                }
            }
            catch (Exception ex)
            {

                MessageBox.Show("登录失败",ex.Message);
                
            }

        }

    

 
        //查询好友
        private void button5_Click(object sender, EventArgs e)

        {
            try

            {
                StringBuilder builder = new StringBuilder();
                builder.Append("select * from QQ where 1 = 1 ");
                if(textBox5.Text.Length != 0)
                {
                    builder
                    .Append(" and name = '")
                    .Append(textBox5.Text)
                    .Append("'");

                     
                }
                builder.Append("order by id desc");
               

                //创建sda对象
                SqlDataAdapter sda = new SqlDataAdapter(builder.ToString(), conn);
                //创建dataset对象
                DataSet ds = new DataSet();
                //填充ds对象
                sda.Fill(ds);
                //设置表格控件的datasorce属性
                dataGridView1.DataSource = ds.Tables[0];
                //设置标题
                dataGridView1.Columns[0].HeaderText = "编号";
                dataGridView1.Columns[1].HeaderText = "姓名";
                dataGridView1.Columns[2].HeaderText = "密码";
                    
                //设置只读
                dataGridView1.ReadOnly = false;
                //不允许添加行
                dataGridView1.AllowUserToAddRows = false;
                //设置背景
                dataGridView1.BackgroundColor = Color.White;
                //只允许选中单行
                dataGridView1.MultiSelect = false;
                //整个行选中
              //  dataGridView1.SelectionMode = DataGridViewSelectionMode.FullColumnSelect;

            }
            catch(Exception ex)
            {
                MessageBox.Show("查询失败" + ex.Message);
            }


            

        }

        private Button button1;
        private Label label1;
        private TextBox textBox1;
        private TextBox textBox2;
        private Label label2;
        private Button button2;
        private Button button3;
        private TextBox textBox3;
        private Label label3;
        private TextBox textBox4;
        private Label label4;
        private Label label5;
        private Button button4;
        private Button button5;
        private Button button6;
        private Button button7;
        private DataGridViewTextBoxColumn idDataGridViewTextBoxColumn;
        private DataGridViewTextBoxColumn nameDataGridViewTextBoxColumn;
        private DataGridViewTextBoxColumn pwdDataGridViewTextBoxColumn;

        private void InitializeComponent()
        {
            this.button1 = new System.Windows.Forms.Button();
            this.label1 = new System.Windows.Forms.Label();
            this.textBox1 = new System.Windows.Forms.TextBox();
            this.textBox2 = new System.Windows.Forms.TextBox();
            this.label2 = new System.Windows.Forms.Label();
            this.button2 = new System.Windows.Forms.Button();
            this.button3 = new System.Windows.Forms.Button();
            this.textBox3 = new System.Windows.Forms.TextBox();
            this.label3 = new System.Windows.Forms.Label();
            this.textBox4 = new System.Windows.Forms.TextBox();
            this.label4 = new System.Windows.Forms.Label();
            this.label5 = new System.Windows.Forms.Label();
            this.button4 = new System.Windows.Forms.Button();
            this.button5 = new System.Windows.Forms.Button();
            this.button6 = new System.Windows.Forms.Button();
            this.button7 = new System.Windows.Forms.Button();
            this.idDataGridViewTextBoxColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.nameDataGridViewTextBoxColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.pwdDataGridViewTextBoxColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.dataGridView1 = new System.Windows.Forms.DataGridView();
            this.textBox5 = new System.Windows.Forms.TextBox();
            this.button8 = new System.Windows.Forms.Button();
            this.button9 = new System.Windows.Forms.Button();
            ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
            this.SuspendLayout();
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(492, 453);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(75, 23);
            this.button1.TabIndex = 0;
            this.button1.Text = "链接数据库";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(28, 31);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(29, 12);
            this.label1.TabIndex = 1;
            this.label1.Text = "账号";
            // 
            // textBox1
            // 
            this.textBox1.Location = new System.Drawing.Point(90, 28);
            this.textBox1.Name = "textBox1";
            this.textBox1.Size = new System.Drawing.Size(100, 21);
            this.textBox1.TabIndex = 2;
            // 
            // textBox2
            // 
            this.textBox2.Location = new System.Drawing.Point(301, 28);
            this.textBox2.Name = "textBox2";
            this.textBox2.Size = new System.Drawing.Size(100, 21);
            this.textBox2.TabIndex = 4;
            // 
            // label2
            // 
            this.label2.AutoSize = true;
            this.label2.Location = new System.Drawing.Point(239, 31);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(29, 12);
            this.label2.TabIndex = 3;
            this.label2.Text = "密码";
            // 
            // button2
            // 
            this.button2.Location = new System.Drawing.Point(480, 28);
            this.button2.Name = "button2";
            this.button2.Size = new System.Drawing.Size(75, 23);
            this.button2.TabIndex = 5;
            this.button2.Text = "登录";
            this.button2.UseVisualStyleBackColor = true;
            this.button2.Click += new System.EventHandler(this.button2_Click);
            // 
            // button3
            // 
            this.button3.Location = new System.Drawing.Point(480, 76);
            this.button3.Name = "button3";
            this.button3.Size = new System.Drawing.Size(75, 23);
            this.button3.TabIndex = 6;
            this.button3.Text = "注册";
            this.button3.UseVisualStyleBackColor = true;
            this.button3.Click += new System.EventHandler(this.button3_Click);
            // 
            // textBox3
            // 
            this.textBox3.Location = new System.Drawing.Point(301, 74);
            this.textBox3.Name = "textBox3";
            this.textBox3.Size = new System.Drawing.Size(100, 21);
            this.textBox3.TabIndex = 10;
            // 
            // label3
            // 
            this.label3.AutoSize = true;
            this.label3.Location = new System.Drawing.Point(239, 77);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(29, 12);
            this.label3.TabIndex = 9;
            this.label3.Text = "密码";
            // 
            // textBox4
            // 
            this.textBox4.Location = new System.Drawing.Point(90, 73);
            this.textBox4.Name = "textBox4";
            this.textBox4.Size = new System.Drawing.Size(100, 21);
            this.textBox4.TabIndex = 8;
            // 
            // label4
            // 
            this.label4.AutoSize = true;
            this.label4.Location = new System.Drawing.Point(28, 76);
            this.label4.Name = "label4";
            this.label4.Size = new System.Drawing.Size(29, 12);
            this.label4.TabIndex = 7;
            this.label4.Text = "账号";
            // 
            // label5
            // 
            this.label5.AutoSize = true;
            this.label5.Location = new System.Drawing.Point(604, 82);
            this.label5.Name = "label5";
            this.label5.Size = new System.Drawing.Size(29, 12);
            this.label5.TabIndex = 11;
            this.label5.Text = "****";
            // 
            // button4
            // 
            this.button4.Location = new System.Drawing.Point(592, 453);
            this.button4.Name = "button4";
            this.button4.Size = new System.Drawing.Size(75, 23);
            this.button4.TabIndex = 12;
            this.button4.Text = "关闭数据库";
            this.button4.UseVisualStyleBackColor = true;
            this.button4.Click += new System.EventHandler(this.button4_Click);
            // 
            // button5
            // 
            this.button5.Location = new System.Drawing.Point(181, 135);
            this.button5.Name = "button5";
            this.button5.Size = new System.Drawing.Size(75, 23);
            this.button5.TabIndex = 13;
            this.button5.Text = "我的好友";
            this.button5.UseVisualStyleBackColor = true;
            this.button5.Click += new System.EventHandler(this.button5_Click);
            // 
            // button6
            // 
            this.button6.Location = new System.Drawing.Point(280, 135);
            this.button6.Name = "button6";
            this.button6.Size = new System.Drawing.Size(75, 23);
            this.button6.TabIndex = 14;
            this.button6.Text = "删除好友";
            this.button6.UseVisualStyleBackColor = true;
            this.button6.Click += new System.EventHandler(this.button6_Click);
            // 
            // button7
            // 
            this.button7.Location = new System.Drawing.Point(377, 135);
            this.button7.Name = "button7";
            this.button7.Size = new System.Drawing.Size(75, 23);
            this.button7.TabIndex = 15;
            this.button7.Text = "修改好友";
            this.button7.UseVisualStyleBackColor = true;
            this.button7.Click += new System.EventHandler(this.button7_Click);
            // 
            // idDataGridViewTextBoxColumn
            // 
            this.idDataGridViewTextBoxColumn.Name = "idDataGridViewTextBoxColumn";
            // 
            // nameDataGridViewTextBoxColumn
            // 
            this.nameDataGridViewTextBoxColumn.Name = "nameDataGridViewTextBoxColumn";
            // 
            // pwdDataGridViewTextBoxColumn
            // 
            this.pwdDataGridViewTextBoxColumn.Name = "pwdDataGridViewTextBoxColumn";
            // 
            // dataGridView1
            // 
            this.dataGridView1.AllowUserToOrderColumns = true;
            this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            this.dataGridView1.Location = new System.Drawing.Point(62, 193);
            this.dataGridView1.Name = "dataGridView1";
            this.dataGridView1.RowTemplate.Height = 23;
            this.dataGridView1.Size = new System.Drawing.Size(571, 193);
            this.dataGridView1.TabIndex = 16;
            // 
            // textBox5
            // 
            this.textBox5.Location = new System.Drawing.Point(62, 135);
            this.textBox5.Name = "textBox5";
            this.textBox5.Size = new System.Drawing.Size(100, 21);
            this.textBox5.TabIndex = 17;
            // 
            // button8
            // 
            this.button8.Location = new System.Drawing.Point(463, 135);
            this.button8.Name = "button8";
            this.button8.Size = new System.Drawing.Size(75, 23);
            this.button8.TabIndex = 18;
            this.button8.Text = "派发女友";
            this.button8.UseVisualStyleBackColor = true;
            this.button8.Click += new System.EventHandler(this.button_Click);
            // 
            // button9
            // 
            this.button9.Location = new System.Drawing.Point(544, 135);
            this.button9.Name = "button9";
            this.button9.Size = new System.Drawing.Size(75, 23);
            this.button9.TabIndex = 19;
            this.button9.Text = "清空好友 ";
            this.button9.UseVisualStyleBackColor = true;
            this.button9.Click += new System.EventHandler(this.button9_Click);
            // 
            // QQ
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(715, 488);
            this.Controls.Add(this.button9);
            this.Controls.Add(this.button8);
            this.Controls.Add(this.textBox5);
            this.Controls.Add(this.dataGridView1);
            this.Controls.Add(this.button7);
            this.Controls.Add(this.button6);
            this.Controls.Add(this.button5);
            this.Controls.Add(this.button4);
            this.Controls.Add(this.label5);
            this.Controls.Add(this.textBox3);
            this.Controls.Add(this.label3);
            this.Controls.Add(this.textBox4);
            this.Controls.Add(this.label4);
            this.Controls.Add(this.button3);
            this.Controls.Add(this.button2);
            this.Controls.Add(this.textBox2);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.textBox1);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.button1);
            this.Name = "QQ";
            this.Text = "QQ";
            ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        private DataGridView dataGridView1;

     

        private TextBox textBox5;
        //删除好友
        private void button6_Click(object sender, EventArgs e)
        {
            try
            {   //获取id
                // int id = int.Parse(dataGridView1.SelectedRows[0].ToString());
                String id = dataGridView1.CurrentRow.Cells[0].Value.ToString();
              
                String sql = "delete from QQ where id = {0}";
                sql = String.Format(sql, id);
                SqlCommand cmd = new SqlCommand(sql, conn);
                int result = cmd.ExecuteNonQuery();
                if(result != -1)
                {
                    MessageBox.Show("删除成功");
                }
                //查询好友
             this.button5_Click(sender, e);

            }
            catch (Exception ex)
            {

                MessageBox.Show("删除失败", ex.Message);
            }
        }
        //修改好友
        private void button7_Click(object sender, EventArgs e)
        {
            try
            {


                int id = int.Parse(dataGridView1.CurrentRow.Cells[0].Value.ToString());
                String name = dataGridView1.CurrentRow.Cells[1].Value.ToString();
                String pwd = dataGridView1.CurrentRow.Cells[2].Value.ToString();
                String sql = "update QQ set name = '{0}',pwd = '{1}' where id = {2}";
                sql = String.Format(sql, name, pwd, id);
                SqlCommand cmd = new SqlCommand(sql, conn);
                if (cmd.ExecuteNonQuery() != -1)
                {
                    MessageBox.Show("修改成功 ");
                }
            }
            catch(Exception ex)
            {
                MessageBox.Show("修改失败", ex.Message);
            }

        }

        private Button button8;
        private Button button9;
        //生成好友
        private void button_Click(object sender, EventArgs e)
        {
            try
            {

                String maxSQL = "select isnull(max(id),0) from QQ";
                SqlCommand cmd = new SqlCommand(maxSQL, conn);
                int max = (int)cmd.ExecuteScalar();
                for (int i = max + 1; i < max + 10; i++)
                {
                    String sql = "insert into QQ (name,pwd) values('{0}','{1}')";
                    sql = String.Format(sql, "我的第" + i + "个女友", "我们在一起的第" + i + "天");
                     cmd = new SqlCommand(sql, conn);
                    if (cmd.ExecuteNonQuery() == -1)
                    {
                        MessageBox.Show("第{0}号女友生成失败", i.ToString());

                    }
                }
                MessageBox.Show("女友生成成功");
                this.button5_Click(sender, e);
                    
            }
            catch(Exception ex)
            {
                MessageBox.Show("女友生成失败", ex.Message);
            }
            
        }
        //清空好友
        private void button9_Click(object sender, EventArgs e)
        {
            try
            {
                DialogResult dr = MessageBox.Show("手贱吗", "危险操作", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
                if(dr == DialogResult.No)
                {
                    MessageBox.Show("恭喜你还保留一丝人性！！！");
                    return;
                }
                String sql = "truncate table QQ";
                SqlCommand cmd = new SqlCommand(sql,conn);
                
                if(cmd.ExecuteNonQuery() != -1)
                {
                    MessageBox.Show("清空失败");
                }
                MessageBox.Show("清空成功");
                button5_Click(sender, e);
            }
            catch (Exception ex)
            {

                MessageBox.Show("清空失败", ex.Message);
            }
            
        }
    }
}
